<?php

namespace app\front\controller;

use app\front\model\info\Info as InfoModel;
use app\front\model\menu\Menu as MenuModel;
use think\facade\Request;
use think\facade\View;

/**
 * 页面控制器
 */
class Info extends Controller
{
    /**
     * 首页
     */
    public function index($menu_id = 0)
    {
        $data = Request::param();
        if ($menu_id == 0) {
            return View::fetch('/index', []);
        }
        $MenuModel = new MenuModel();
        $InfoModel = new InfoModel;
        //栏目详情
        $menuInfo = $MenuModel::detail($menu_id);
        $data['list_rows'] = 10;
        //内容列表
        $infoList = $InfoModel->getList($menu_id, $menuInfo, $data);
        $total = in_array($menuInfo['mould_id'], [1, 2, 3]) && count($infoList) > 0 ? $infoList->total() : 0;
        $totalpage = $total > 0 ? ceil($total / $data['list_rows']) : 0;
        $page = isset($data['page']) ? $data['page'] : 1;
        $uppage = $page - 1;
        $dowpage = $page + 1;
        $columnList = $MenuModel->getlevelMenu($menu_id, $menuInfo);
        //输出模板
        $template = "list_" . $menuInfo['mould']['table'];

        //获取上级位置
        $currentMenu = $MenuModel->detail($menuInfo['parent_id']);
        return View::fetch('/' . $template,
            [
                'columnList' => $columnList,
                'infoList' => $infoList,
                'menuInfo' => $menuInfo['parent_id'] > 0 ? $currentMenu : $menuInfo,
                'currentMenu' => $menuInfo,
                'parent_id' => $menuInfo['parent_id'] > 0 ? $menuInfo['parent_id'] : $menu_id,
                'total' => $total,
                'totalpage' => $totalpage,
                'uppage' => $uppage,
                'dowpage' => $dowpage,
                'page' => $page,
            ]);
    }

    /**
     * 详情
     */
    public function detail($info_id)
    {
        $InfoModel = new InfoModel;
        //栏目详情
        $detail = $InfoModel::detail($info_id);

        $MenuModel = new MenuModel();
        $menuInfo = $MenuModel::detail($detail['menu_id']);
        //获取上级位置
        $currentMenu = $MenuModel->detail($menuInfo['parent_id']);

        $columnList = $MenuModel->getlevelMenu($detail['menu_id'], $menuInfo);
        //输出模板
        $template = "view_" . $detail['mould']['table'];
        //上一篇
        $lastInfo = $InfoModel->getLastDetail($detail);
        //下一篇
        $nextInfo = $InfoModel->getNextDetail($detail);
        return View::fetch('/' . $template,
            [
                'menuInfo' => $menuInfo['parent_id'] > 0 ? $currentMenu : $menuInfo,
                'currentMenu' => $menuInfo,
                'columnList' => $columnList,
                'detail' => $detail,
                'lastInfo' => $lastInfo,
                'nextInfo' => $nextInfo,
            ]);
    }

    /**
     * 搜索
     */
    public function search()
    {
        $data = Request::param();
        $InfoModel = new InfoModel;
        $data['list_rows'] = 10;
        $list = $InfoModel->getSearchList($data);
        $total = count($list) > 0 ? $list->total() : 0;
        $totalpage = ceil($total / $data['list_rows']);
        $page = isset($data['page']) ? $data['page'] : 1;
        $uppage = $page - 1;
        $dowpage = $page + 1;
        return View::fetch('/search',
            [
                'infoList' => $list,
                'total' => $total,
                'totalpage' => $totalpage,
                'uppage' => $uppage,
                'dowpage' => $dowpage,
                'page' => $page,
                'search' => $data['search']
            ]);
    }
}
